package com.jl15988.simpletools.system.controller;

import com.jl15988.simpletools.common.annotation.Log;
import com.jl15988.simpletools.common.constant.OperationType;
import com.jl15988.simpletools.common.utils.Result;
import com.jl15988.simpletools.system.domain.dto.MenuDTO;
import com.jl15988.simpletools.system.domain.dto.MenuQueryDTO;
import com.jl15988.simpletools.system.domain.entity.Menu;
import com.jl15988.simpletools.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单控制器
 *
 * @author jl15988
 */
@Slf4j
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/api/v1/menus")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("获取菜单列表")
    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<List<Menu>> getMenuList(MenuQueryDTO query) {
        return Result.success(menuService.getMenuList(query));
    }

    @ApiOperation("获取菜单树")
    @GetMapping("/tree")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<List<MenuDTO>> getMenuTree(MenuQueryDTO query) {
        return Result.success(menuService.getMenuTree(query));
    }

    @ApiOperation("获取菜单详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<MenuDTO> getMenuDetail(@PathVariable Long id) {
        return Result.success(menuService.getMenuDetail(id));
    }

    @ApiOperation("创建菜单")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "创建菜单", operationType = OperationType.CREATE)
    public Result<Long> createMenu(@Validated @RequestBody MenuDTO menu) {
        return Result.success(menuService.createMenu(menu));
    }

    @ApiOperation("更新菜单")
    @PutMapping
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "更新菜单", operationType = OperationType.UPDATE)
    public Result<Boolean> updateMenu(@Validated @RequestBody MenuDTO menu) {
        return Result.success(menuService.updateMenu(menu));
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "删除菜单", operationType = OperationType.DELETE)
    public Result<Boolean> deleteMenu(@PathVariable Long id) {
        return Result.success(menuService.deleteMenu(id));
    }

    @ApiOperation("根据角色ID获取菜单树")
    @GetMapping("/role/{roleId}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<List<MenuDTO>> getMenuTreeByRoleId(@PathVariable Integer roleId) {
        return Result.success(menuService.getMenuTreeByRoleId(roleId));
    }

    @ApiOperation("根据角色ID获取菜单ID列表")
    @GetMapping("/role/{roleId}/ids")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<List<Long>> getMenuIdsByRoleId(@PathVariable Integer roleId) {
        return Result.success(menuService.getMenuIdsByRoleId(roleId));
    }

    @ApiOperation("分配角色菜单权限")
    @PostMapping("/role/{roleId}")
    @PreAuthorize("hasRole('ADMIN')")
    @Log(title = "分配角色菜单权限", operationType = OperationType.UPDATE)
    public Result<Boolean> assignRoleMenus(@PathVariable Integer roleId, @RequestBody List<Long> menuIds) {
        return Result.success(menuService.assignRoleMenus(roleId, menuIds));
    }
} 